package com.finchina.myview;

import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;

/**
 * 作者：黄赛赛 on 2015/9/9 09:17
 *
 * @version V1.0
 * @Description: ${todo}()
 */
public class CustomProgressBar extends View {
    final String TAG = "CustomProgressBar";
    /**
     * 第一圈的颜色
     */
    private int mFirstColor;
    /**
     * 第二圈的颜色
     */
    private int mSecondColor;
    /**
     * 圈的宽度
     */
    private int mCircleWidth;
    /**
     * 画笔
     */
    private Paint mPaint;
    /**
     * 当前进度
     */
    private int mProgress;

    /**
     * 速度
     */
    private int mSpeed;
    /**
     * 是否应该开始下一个
     */
    private boolean isNext = false;

    /**
     * 进度的字体颜色
     */
    int progressTextColor;

    /**
     * 进度的文字大小
     */
    int progressTextSize;


    int curProgress = 0;

    Rect textRect;

    public CustomProgressBar(Context context) {
//        super(context);
        this(context, null);
    }

    public CustomProgressBar(Context context, AttributeSet attrs) {
//        super(context, attrs);
        this(context, attrs, 0);
    }

    public CustomProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        TypedArray ta = getContext().getTheme().obtainStyledAttributes(attrs, R.styleable.CustomProgressBar, defStyleAttr, 0);

        mFirstColor = ta.getColor(R.styleable.CustomProgressBar_firstColor, Color.GREEN);
        mSecondColor = ta.getColor(R.styleable.CustomProgressBar_secondColor, Color.RED);
        mCircleWidth = ta.getDimensionPixelOffset(R.styleable.CustomProgressBar_circleWidth, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10, getResources().getDisplayMetrics()));
        mSpeed = ta.getInt(R.styleable.CustomProgressBar_speed, 10);
        progressTextColor = ta.getColor(R.styleable.CustomProgressBar_progressTextColor, Color.CYAN);
        progressTextSize = ta.getDimensionPixelSize(R.styleable.CustomProgressBar_progressTextSize, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 16, getResources().getDisplayMetrics()));
        ta.recycle();

        mPaint = new Paint();

        mPaint.setTextSize(progressTextSize);
        mPaint.setStyle(Paint.Style.FILL);
        textRect = new Rect();
        String text = "100%";
        mPaint.getTextBounds(text, 0, text.length(),textRect);

        // 绘图线程
//        new Thread()
//        {
//            public void run()
//            {
//                while (true)
//                {
//                    mProgress++;
//                    if (mProgress == 360)
//                    {
//                        mProgress = 0;
//                        if (!isNext)
//                            isNext = true;
//                        else
//                            isNext = false;
//                    }
//                    postInvalidate();
//                    try
//                    {
//                        Thread.sleep(mSpeed);
//                    } catch (InterruptedException e) {
//                        e.printStackTrace();
//                    }
//                }
//            };
//        }.start();


//        ValueAnimator va = ValueAnimator.ofObject(new ProgressEvaluate(), 0,360);
//        va.setDuration(5000);
//        va.setRepeatMode(ValueAnimator.REVERSE);
//        va.setRepeatCount(Integer.MAX_VALUE);
//        va.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
//            @Override
//            public void onAnimationUpdate(ValueAnimator animation) {
//                mProgress = (int) animation.getAnimatedValue();
////                mProgress++;
//                if (mProgress == 360)
//                {
//                    mProgress = 0;
//                    if (!isNext)
//                        isNext = true;
//                    else
//                        isNext = false;
//                }
//                invalidate();
//            }
//        });
//        va.start();


        ValueAnimator va = ValueAnimator.ofInt(0,360);

        va.setDuration(4000);

        va.setRepeatMode(ValueAnimator.REVERSE);
        va.setRepeatCount(Integer.MAX_VALUE);
        va.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                mProgress = (int) animation.getAnimatedValue();
                if (mProgress == 360)
                {
                    mProgress = 0;
                    if (!isNext)
                        isNext = true;
                    else
                        isNext = false;
                }
                invalidate();
            }
        });
        va.start();
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);

        int width=0, height =0;

        int textMax = Math.max(textRect.width(), textRect.height());
        if (widthMode == MeasureSpec.EXACTLY){
            width = widthSize;
        }else {
            int desire = getPaddingLeft() + getPaddingRight() + mCircleWidth*2+textMax;
            width = Math.min(desire, widthSize);
        }

        if (heightMode == MeasureSpec.EXACTLY){
            height = heightSize;
        }else {
            int desire = getPaddingTop() + getPaddingBottom() + mCircleWidth*2 + textMax;
            height = Math.min(desire,heightSize);
        }


        setMeasuredDimension(width,height);
    }

    @Override
    protected void onDraw(Canvas canvas) {
//        super.onDraw(canvas);
        int centerX = getMeasuredWidth()/ 2;
        int centerY = getMeasuredHeight() / 2;
        int des_radius = Math.min(centerX,centerY);
        int radius =  des_radius - mCircleWidth;


        mPaint.setColor(Color.YELLOW);
        mPaint.setStyle(Paint.Style.FILL);
        canvas.drawRect(0,0,getMeasuredWidth(),getMeasuredHeight(),mPaint);

        mPaint.setStrokeWidth(mCircleWidth);
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.STROKE);


        // 用于定义的圆弧的形状和大小的界限
        RectF oval = new RectF(centerX - radius, centerY - radius, centerX+radius, centerY + radius);
        if (!isNext){// 第一颜色的圈完整，第二颜色跑
            mPaint.setColor(mFirstColor); // 设置圆环的颜色
            canvas.drawCircle(centerX, centerY, radius, mPaint);// 画出圆环
            mPaint.setColor(mSecondColor);// 设置圆环的颜色
            canvas.drawArc(oval, -90, mProgress, false, mPaint);// 根据进度画圆弧
        }else {
            mPaint.setColor(mSecondColor); // 设置圆环的颜色
            canvas.drawCircle(centerX, centerY, radius, mPaint); // 画出圆环
            mPaint.setColor(mFirstColor); // 设置圆环的颜色
            canvas.drawArc(oval, -90, mProgress, false, mPaint); // 根据进度画圆弧
        }

        mPaint.setColor(progressTextColor);
        String text = mProgress*100 /360 +"%";
        mPaint.setTextSize(progressTextSize);
        mPaint.setStyle(Paint.Style.FILL);
        Rect textRect = new Rect();
        mPaint.getTextBounds(text, 0, text.length(),textRect);

        float textX= centerX- (textRect.width()/2);
        float  textY = centerY + (textRect.height()/2);
//        Log.e(TAG, "centerX = "+centerX);
//        Log.e(TAG, "centerY =" +centerY);
//        Log.e(TAG, "textRect.width() = "+textRect.width());
//        Log.e(TAG, "textRect.height() = "+textRect.height());
//        Log.e(TAG, "textX = "+textX);
//        Log.e(TAG, "textY =" +textY);
        canvas.drawText(text, 0, text.length(), textX, textY, mPaint);

    }
}

